package com.ruoyi.lab.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.lab.domain.JacLabSupplyFile;
import com.ruoyi.lab.mapper.JacLabSupplyFileMapper;
import com.ruoyi.lab.service.IJacLabSupplyFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SupplyFileService业务层处理
 * @author sxx
 * @date 2021-05-24
 */

@Service
public class JacLabSupplyFileServiceImpl extends BaseController implements IJacLabSupplyFileService
{
    @Autowired
    private JacLabSupplyFileMapper jacLabSupplyFileMapper;

    String OK = "1";

    /**
     * 查询SupplyFile
     * @param id SupplyFileID
     * @return SupplyFile
     */
/*    @Override
    public JacLabSupplyFile selectJacLabSupplyFileById(Long id)
    {
        return jacLabSupplyFileMapper.selectJacLabSupplyById(id);
    }*/

    /**
     * 查询SupplyFile列表
     * 通过时间节点去查询
     * @param jacLabSupplyFile SupplyFile
     * @return SupplyFile
     */
    @Override
    public Map<String, Object> selectJacLabSupplyFileList(JacLabSupplyFile jacLabSupplyFile)
    {
        HttpServletRequest request = ServletUtils.getRequest();
        Long role = jacLabSupplyFileMapper.selectRole("试验测试试验院管理员");
        String usertype = jacLabSupplyFileMapper.selectUserTypeById(request.getHeader(Constants.CURRENT_ID));
        Long permissions = jacLabSupplyFileMapper.selectPermissions(request.getHeader(Constants.CURRENT_ID),role);
        if (usertype.equals("00")){
            Map<String, Object> map1 = new HashMap<>();
            List<JacLabSupplyFile> newList = jacLabSupplyFileMapper.selectJacLabSupplyList(jacLabSupplyFile);
            map1.put("total",newList.size());
            startPage();
            List<JacLabSupplyFile> list = jacLabSupplyFileMapper.selectJacLabSupplyList(jacLabSupplyFile);
            List<JacLabSupplyFile> resultList = new ArrayList<>();
            for (JacLabSupplyFile l:list) {
                List<JacLabSupplyFile> list1 = jacLabSupplyFileMapper.selectJacLabSupplyFileList(l);
                if (list1.size() != 0) {
                    List<Map<String, String>> list2 = new ArrayList<>();
                    for (JacLabSupplyFile ll : list1) {
                        Map<String, String> map = new HashMap<>();
                        map.put("fileId", ll.getFileid());
                        map.put("fileName", ll.getFilename());
                        list2.add(map);
                    }
                    l.setFileListnew(list2);
                }
                resultList.add(l);
            }
            map1.put("permissions", permissions);
            map1.put("list", resultList);
            return map1;
        }else {
            Map<String, Object> map1 = new HashMap<>();
            List<JacLabSupplyFile> newList = jacLabSupplyFileMapper.selectJacLabSupplyListBySupply(jacLabSupplyFile);
            map1.put("total",newList.size());
            startPage();
            List<JacLabSupplyFile> list = jacLabSupplyFileMapper.selectJacLabSupplyListBySupply(jacLabSupplyFile);
            List<JacLabSupplyFile> resultList = new ArrayList<>();
            for (JacLabSupplyFile l:list) {
                List<JacLabSupplyFile> list1 = jacLabSupplyFileMapper.selectJacLabSupplyFileList(l);
                if (list1.size() != 0) {
                    List<Map<String, String>> list2 = new ArrayList<>();
                    for (JacLabSupplyFile ll : list1) {
                        Map<String, String> map = new HashMap<>();
                        map.put("fileId", ll.getFileid());
                        map.put("fileName", ll.getFilename());
                        list2.add(map);
                    }
                    l.setFileListnew(list2);
                }
                resultList.add(l);
            }
            map1.put("permissions", permissions);
            map1.put("list", resultList);
            return map1;
        }
    }


    /**
     * 新增SupplyFile
     * @param jacLabSupplyFile SupplyFile
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
    public String insertJacLabSupplyFile(JacLabSupplyFile jacLabSupplyFile)
    {
        String result = OK;
        try {
            jacLabSupplyFileMapper.insertJacLabSupply(jacLabSupplyFile);
            Long oid = jacLabSupplyFileMapper.selectJacLabSupplyBySupplyId(jacLabSupplyFile.getDescribe());
            try {
                List<Map<String, String>> filelist = jacLabSupplyFile.getFileListnew();
                for (Map<String, String> l : filelist) {
                    if (jacLabSupplyFileMapper.selectJacLabSupplyFileListHas(oid,l.get("fileId"))){
                        result = "文件已存在，请检查！";
                        return result;
                    }
                }
                for (Map<String, String> l : filelist) {
                    jacLabSupplyFileMapper.insertJacLabSupplyFile(oid,l.get("fileId"),l.get("fileName"));
                }
            } catch (Exception a){
                result = "上传文件失败！";
            }
        } catch (Exception e) {
            result = "保存报告失败！";
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }


    /**
     * 修改SupplyFile
     */
    @Async
    @Override
    public String updateJacLabSupplyFile(JacLabSupplyFile jacLabSupplyFile)
    {
        String result = OK;
        try {
            List<Map<String, String>> filelist = jacLabSupplyFile.getFileListnew();
            for (Map<String, String> l : filelist) {
                if (jacLabSupplyFileMapper.selectJacLabSupplyFileListHas(jacLabSupplyFile.getId(),l.get("fileId"))) {
                    result = "文件已存在，请检查！";
                    return result;
                }
            }
            for (Map<String, String> l : filelist) {
                jacLabSupplyFileMapper.insertJacLabSupplyFile(jacLabSupplyFile.getId(),l.get("fileId"),l.get("fileName"));
            }
        } catch (Exception E){
            result = "追加文件失败！";
        }
        return result;
    }

    /**
     * 修改SupplyFile权限
     */
    @Override
    public String updateJacLabSupplyFilePower(JacLabSupplyFile jacLabSupplyFile)
    {
        String result = OK;
        try {
            jacLabSupplyFileMapper.updateJacLabSupplyFilePower(jacLabSupplyFile);
        } catch (Exception e){
            result = "更新权限失败！";
        }
        return result;
    }

    /**
     * 删除SupplyFile
     */
    @Override
    public String deleteJacLabSupplyFileByIds(String ids)
    {
        String result = OK;
        Long[] longs = Convert.toLongArray(ids);
        for (Long l:longs){
            jacLabSupplyFileMapper.deleteJacLabSupplyByIds(l);
            jacLabSupplyFileMapper.deleteJacLabSupplyFileByIds(l);
        }
        return result;
    }

    /**
     * 删除SupplyFile单个文件
     * @param fileId SupplyFileID
     * @return 结果
     */
    public String deleteJacLabSupplyFileById(String fileId)
    {
        String result = OK;
        jacLabSupplyFileMapper.deleteJacLabSupplyFileById(fileId);
        return result;
    }
}
